﻿/*----------------------------------------------------------------
//Copyright (C) 2012 党明
//文件功能描述：购物车相关操作
//创建人：党明
//生成时间：2012/04/27 22:47:00
//----------------------------------------------------------------*/
var backUrl = "";

$(document).ready(function () {
    LoadMyShoppingCart();
    BindShoppingCartEvent();
    $("#goshopping").bind("click", function () { location.href = (backUrl == "" ? "/index.aspx" : backUrl); });
});


function LoadMyShoppingCart() {
    var url = "/home/as/shoppingcartcommon.aspx?Action=ShowShoppingCart&r=" + Math.random();
    $.ajax(
    {
        type: "GET",
        url: url,
        success: function (ret) {
            $(".showshoppingCart").html(ret);
            //$("input[name='txtQuantity']", $(ret)).bind("blur", CalcCountAndPrice);
            $(".delop").bind("click", DeleteShoppingCart);
        }
    });
}

function CalcCountAndPrice() {
    var quantity = $("input[name='txtQuantity']");
    var price = $("input[name='txtprice']");
    if (quantity.length != price.length) {
        $(".totalcount").html("0");
        $(".totalprice").html("0.0");
        return;
    }
    var totalcount = 0;
    $(quantity).each(function () {
        totalcount += parseInt($(this).val());
    });
    var totalprice = 0.0;
    $(price).each(function (i) {
        totalprice += parseFloat($(this).val()) * parseFloat($(quantity).eq(i).val());
    });
    $(".totalcount").html(totalcount);
    $(".totalprice").html(parseFloat(totalprice).toFixed(2));

}

function BindShoppingCartEvent() {

    $("#clearShopping").bind("click", DeleteAllShoppingCart);
    $("#btnPay").bind("click", function () {
        window.location.href = "/home/orderinfo.aspx";
    });
}

function DeleteShoppingCart() {
    var goodsid = $(this).attr("tag");
    if (goodsid == "") {
        alert("请选择要删除的购物车的商品");
        return;
    }
    $this = $(this).parents(".shoppingrow");
    var url = "/home/as/shoppingcartcommon.aspx?Action=delshopping&r=" + Math.random();
    var data1 = "goodsid=" + goodsid;
    $.ajax(
    {
        type: "POST",
        url: url,
        data: data1,
        dataType: "json",
        success: function (ret) {
            if (ret.Result == 1) {
                //删除成功 把购物车商品隐藏掉先，然后再删除该商品信息
                $this.remove();
                CalcCountAndPrice()
            }
            else {
                alert(ret.Message);
            }
        }
    });
}
function DeleteAllShoppingCart() {
    var url = "/home/as/shoppingcartcommon.aspx?Action=DeleteAllShopping&r=" + Math.random();

    $.ajax(
    {
        type: "POST",
        url: url,

        dataType: "json",
        success: function (ret) {
            if (ret.Result == 1) {
                $(".showshoppingCart").html("");
                //CalcCountAndPrice()
            }
            else {
                alert(ret.Message);
            }
        }
    });
}



function ModifyQuantity() {
    var url = "/home/as/shoppingcartcommon.aspx?Action=ModifyQuantity&GoodsID=" + $("#hfGoodsID").val() + "&Quantity=" + $("#txtQuantity").val() + "&r=" + Math.random();

    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (ret) {
            if (ret.Result == 1) {
                //$(".showshoppingCart").html("");
                CalcCountAndPrice();
            }
            else {
                alert(ret.Message);
            }
        }
    });
}

